﻿using System;

using Microsoft.Phone.Controls;

namespace NavigationCoercion.Actions
{
    internal class RedirectWithReturnUriAction : RedirectActionBase
    {
        private readonly string _page;
        private readonly string _keyName;

        public RedirectWithReturnUriAction(Type pageType, string key)
        {
            this._page = pageType.GetPossibleXamlPagePath();
            this._keyName = key;
        }

        internal override void PerformRedirect(PhoneApplicationFrame frame)
        {
            frame.Dispatcher.BeginInvoke(() =>
                {
                    var uri = string.Format("{0}&{1}={2}", this._page, this._keyName, frame.CurrentSource);
                    frame.Navigate(new Uri(uri, UriKind.Relative));
                });
        }
    }
}